Open file in browser instead of downloading 您所在的位置:网站首页 display view pdf file without downloading using Open file in browser instead of downloading

Open file in browser instead of downloading

2023-07-14 00:16| 来源: 网络整理| 查看: 265

Open file in browser instead of downloading open file in browser without downloading

Today in this article, we will simple technique of how to Open file in browser instead of downloading it directly in the .NET Core-based ASP or MVC application or API application.

You may want to force files to open in the client’s browser before a client can actually download them directly. We shall see how to achieve this for the .PDF or Image file like .png or .jpeg file.

Please note that ASP.NET Core now supports file download. You can let User open the file directly or download it directly without opening it in the browser.

Today in this article, we will cover below aspects,

Using File class to Open PDF file in browserUsing FileContentResult class to Open file in browserUsing File class to Open Image/png file in browserDifference between FileResult, FileContentResult FileStreamResult

If you are already sure about the file extension and you want the user to open the file as a preview then we can use the File or FileContentResult class to decide the behavior.

Using File class to Open PDF file in browser

File class can be used directly in the controller method once you obtain the stream or byte array of the required file.

[HttpGet("{id}")] [Route("Stream")] public async Task DownloadInBrowserPdfFile(string fileId) { byte [] stream = await _fileInterafces.ReadAsByteStream(fileId); string mimeType = "application/pdf"; return File(stream, mimeType); } Using FileContentResult class to Open file in browser

FileContentResult class can be used directly in the controller method once you obtain the stream or byte array of the required file.

[HttpGet("{id}")] [Route("Stream")] public async Task DownloadInBrowserPdfFile(string fileId) { byte [] stream = await _fileInterafces.ReadAsByteStream(fileId); string mimeType = "application/pdf"; return new FileContentResult(stream, mimeType); }

Using File class to Open Image/png file in browser

Similarly depending on the file type, if you are available, you can specify file type,

[HttpGet("{id}")] [Route("Stream")] public async Task DownloadInBrowserPdfFile(string fileId) { byte [] stream = await _fileInterafces.ReadAsByteStream(fileId); string mimeType = "image/png"; return new FileContentResult(stream, mimeType); }

make a file open in browser instead of downloading

Difference between FileResult, FileContentResult FileStreamResult

As we know FileResult is an abstract base class. Both FileContentResult & FileStreamResult classes are derived class from Abstract FileResult class.

FileStreamResult – Sends binary content to the response by using a Stream instance. Here you have a stream and want to return stream content as a file.

FileContentResult – Sends the contents of a binary file to the response. Here you have a byte array and want to return byte content as a file.

References:

Return or Download File in ASP.NET Core API

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.

References:

Python – How to check if a file existsC# Read Excel file As JSONHow to Add JSON to Git Readme markup fileReturn or Download File in ASP.NET Core APIC# Read CSV file in .NET Core -TextFieldParser

Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published best practices and guidelines for software design and development.

Growing by Sharing

You might be interested :Split large File in Chunks using File Enumerator- Approach 3Retrieve a file from a server via SFTP-.NET C#File Error -The process cannot access the file because it is…


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有